package com.jingyuan.client;

import com.jingyuan.common.entity.HostInfo;
import com.jingyuan.common.entity.ServerMessage;
import com.jingyuan.common.entity.packet.ServerMessageType;
import com.jingyuan.common.entity.packet.VotePacket;
import io.netty.channel.*;

import java.util.Collections;
import java.util.List;
import java.util.Random;

public class NettyClientHandler extends SimpleChannelInboundHandler<ServerMessage> {
    private NettyHarmonyServiceImpl service;

    public NettyClientHandler(NettyHarmonyServiceImpl service){
        this.service=service;
    }

    private HostAddressManager manager=HostAddressManager.getHostAddressManager();
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ServerMessage response) throws Exception {
        System.out.println(
                "------------------------Client received message-------------------------");
        System.out.println(response.getMsg());
        //收到本机上线确认报文
        int responseMsgType=response.getServerMessageType();
        if(responseMsgType==ServerMessageType.OTHER_HOST_REGIST_SUCCESS){
            HostInfo info=response.getHost();
            manager.addHost(info);  //添加进地址管理器中
        }else if(responseMsgType==ServerMessageType.REGIST_SUCCESS){
            //本机上线时，收到的其他在线主机信息通知
            List<HostInfo> hostInfos= response.getOnlinehHostList();
            if(hostInfos!=null&&!hostInfos.isEmpty()){
                System.out.println(response.getOnlinehHostList());
                manager.addHost(response.getOnlinehHostList());
            }
        }else if(responseMsgType==ServerMessageType.OTHER_OFFLINE_SUCCESS){
            manager.removeHost(response.getHost().getAddress());  //移除主机
        }else if((responseMsgType==ServerMessageType.VOTING_START)||(responseMsgType==ServerMessageType.VOTING_NOW)){
            //选举阶段，发送自己参与选举的报文
            VotePacket votePacket=new VotePacket();
            Random random=new Random();
            votePacket.setRandomNumber(random.nextInt());
            service.vote(votePacket);
            manager.setOnVoting(true);
        }else if(response.getServerMessageType()==ServerMessageType.VOTING_END){
            //选举完成
            HostInfo hostInfo=response.getHost();
            String masterAddress=hostInfo.getAddress();
            int masterPort=hostInfo.getPort();
            manager.setMasterAddress(masterAddress);
            manager.setMasterPort(masterPort);
            manager.setOnVoting(false);
            String localServiceAddress=PropertiesUtil.getPropertiesUtil().getHostAddress();
            int localPort= PropertiesUtil.getPropertiesUtil().getPort();
            if(masterAddress.equals(localServiceAddress)&&masterPort==localPort){
                manager.setMaster(true);        //本机成功被选举为Master节点
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        cause.printStackTrace();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        service.setChannel(ctx.channel());

    }
}
